package pun.potato;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.FacebookRequestError;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphObject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

public class SplashScreen extends Activity {
	
	ImageView logo;
	private String requestId;
	private String facebookId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        
		Uri intentUri = this.getIntent().getData();
		if(intentUri != null){
			String requestIdParam = intentUri.getQueryParameter("request_ids");
			if(requestIdParam != null){
				String array[] = requestIdParam.split(",");
				requestId = array[0];
				//Log.i("MainFragment", "Request id: " + requestId);
				getRequestData(requestId);
				//Toast.makeText(getApplicationContext(), "App started from facebook app", Toast.LENGTH_LONG).show();
			}
		}
        
        
        TimerTask task = new TimerTask(){
        	public void run(){
        		//startActivity(new Intent(SplashScreen.this, MainMenu.class));
        		Intent intent = new Intent(SplashScreen.this, LoginActivity.class); 
				intent.putExtra("facebookId", facebookId);
        		startActivity(intent);
        		SplashScreen.this.finish();
        	}
        };
        
        Timer timer = new Timer();
        timer.schedule(task, 3500);
        
        //Add some animation to the splash screen here
//        AnimationSet animationSet = new AnimationSet(true);
//        logo = (ImageView)findViewById(R.id.imageView1);
//        TranslateAnimation anim1 = new TranslateAnimation(0, 0, -400, 0);
//        anim1.setDuration(1000);
//        animationSet.addAnimation(anim1);
//        
//        TranslateAnimation anim2 = new TranslateAnimation(0, 0, 200, 0);
//        anim2.setDuration(500);
//        anim2.setStartOffset(1000);
//        animationSet.addAnimation(anim2);
//      
//        logo.startAnimation(animationSet);
        
    }
    
    private void getRequestData(final String inRequestId){
    	//create a new request for an http get with the request id as the graph path
    	Request request = new Request(Session.getActiveSession(), inRequestId, null, HttpMethod.GET, new Request.Callback() {
			
			@Override
			public void onCompleted(Response response) {
				GraphObject graphObject = response.getGraphObject();
				if(graphObject != null){
					JSONObject fromObject = (JSONObject)graphObject.getProperty("from");
					try {
						facebookId = fromObject.getString("id");
						deleteRequest(inRequestId);
					} catch (JSONException e) {
						facebookId = "";
					}
					//Log.d("SplashScreen", sender);
					//Toast.makeText(getApplicationContext(), sender, Toast.LENGTH_LONG).show();
				}
				
			}
		});
    	Request.executeBatchAsync(request);
    }
    
    private void deleteRequest(String inRequestId) {
        // Create a new request for an HTTP delete with the
        // request ID as the Graph path.
        Request request = new Request(Session.getActiveSession(), 
            inRequestId, null, HttpMethod.DELETE, new Request.Callback() {

                @Override
                public void onCompleted(Response response) {
                    // Show a confirmation of the deletion
                    // when the API call completes successfully.
                    //Toast.makeText(getApplicationContext(), "Request deleted",
                    //Toast.LENGTH_SHORT).show();
                }
            });
        // Execute the request asynchronously.
        Request.executeBatchAsync(request);
    }
    
    
}
